package cn.tedu.ivos.application.controller;

import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/application")
public class ApplicationController {
    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private VehicleMapper vehicleMapper;
    @PostMapping("/save")
    public JsonResult saveApplication(@RequestBody ApplicationParam applicationParam){
        log.debug("开始处理【保存申请单】的请求，参数：{}",applicationParam);
        int rows =applicationService.saveApplication(applicationParam);
        if(rows==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"保存失败");
        }
        return JsonResult.ok();
    }
    @GetMapping("/select")
    public JsonResult selectApplication(ApplicationQuery applicationQuery){
        log.debug("开始处理【查询申请单】的请求，参数：{}",applicationQuery);
        List<ApplicationVO> list = applicationService.selectApplication(applicationQuery);
        log.debug("查询申请单的结果：{}",list);
        return JsonResult.ok(list);
    }
    @PostMapping("/cancel/{id}")
    public JsonResult cancelApplication(@PathVariable Long id){
        log.debug("开始处理【取消申请单】的请求，参数：{}",id);
        ApplicationParam applicationParam=new ApplicationParam();
        applicationParam.setId(id);
        int rows =applicationService.cancelApplication(applicationParam);
        if(rows==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"取消失败");
        }
        return JsonResult.ok();
    }
    @PostMapping("/distribute/{applicationId}/{id}")
    public JsonResult distributeVehicle(@PathVariable Long applicationId,@PathVariable Long id){
        log.debug("开始处理【分配车辆】的请求，参数,申请id:"+applicationId+",车辆id"+id);
        ApplicationParam applicationParam=new ApplicationParam();
        applicationParam.setId(applicationId);
        applicationParam.setVehicleId(id);
        applicationParam.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        int rows =applicationService.saveApplication(applicationParam);
        if(rows==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"分配失败");
        }
        return JsonResult.ok();
    }
    @PostMapping("/returnVehicle")
    public JsonResult returnVehicle(@RequestBody ApplicationParam applicationParam){
        log.debug("开始处理【车辆归还】的请求，参数：{}",applicationParam);
        applicationParam.setStatus(ApplicationStatusEnum.END.getCode());
        int rows =applicationService.saveApplication(applicationParam);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(applicationParam.getVehicleId());
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
        if(rows==0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"归还失败");
        }
        return JsonResult.ok();
    }

}
